package com.youku.model;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.youku.config.XmlReader;

public class LogModel {
	
	private String name;

	private Map<String, String> attribute=new HashMap<>();
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String get(String key){
		return attribute.get(key);
	}
	
	public void put(String key, String value) {
		attribute.put(key, value);
	}

	public Map<String, String> getAttribute() {
		return attribute;
	}

	public void setAttribute(Map<String, String> attribute) {
		this.attribute = attribute;
	}

	@Override
	public String toString() {
		return "LogModel [attribute=" + attribute + "]";
	}

	public static LogModel create(String action, String[] data) {
		String[] fields=XmlReader.getActionValues(action);
		Map<String, String> map = new HashMap<>();
		for (int i = 0; i < fields.length; i++) {
			if (StringUtils.isNotBlank(fields[i])) {
				if (i < data.length) {
					map.put(fields[i], data[i]);
				} else {
					map.put(fields[i], "");
				}
			}
		}
		LogModel model=new LogModel();
		model.attribute=map;
		
		String[] keys=XmlReader.getActionKeys(action);
		StringBuilder builder = new StringBuilder();
		for (String key : keys) {
			builder.append(model.get(key));
		}
		model.name=builder.toString();
		return model;
	}
	
}
